package top.go2do.auth.business.permission.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.go2do.auth.business.permission.module.*;
import top.go2do.auth.business.permission.service.PermissionService;
import top.go2do.common.controller.TomatoController;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.module.pageQuery.PageResult;

import java.util.List;

/**
 * @Description
 * @Author xingyj
 * @CreateTime 2025/6/12 11:52
 */
@Slf4j
@RequestMapping("/perms")
@RestController
public class PermissionController implements TomatoController<PermissionPO, PermissionCreateForm, PermissionModifyForm, Long, PermissionQueryCondition, PermissionQueryAllCondition, PermissionListVO> {

    @Autowired
    private PermissionService permissionService;

    @Override
    public PermissionPO create(PermissionCreateForm createForm, UserInfoBO userInfo) {
        log.info("perms create appId:{}, permName:{}", userInfo.getAppId(), createForm.getPermName());
        createForm.setAppId(userInfo.getAppId());
        createForm.setUserId(userInfo.getUserId());
        return permissionService.create(createForm);
    }

    @Override
    public void modifyById(Long id, PermissionModifyForm modifyForm, UserInfoBO userInfo) {
        modifyForm.setPermId(id);
        modifyForm.setUserId(userInfo.getUserId());
        permissionService.modifyById(modifyForm);
    }

    @Override
    public void removeById(Long id, UserInfoBO userInfo) {
        permissionService.removeById(id);
    }

    @Override
    public PermissionPO getById(Long id) {
        return permissionService.getById(id);
    }

    @Override
    public List<PermissionListVO> listAll(PermissionQueryAllCondition condition) {
        List<PermissionPO> poList = permissionService.listAll(condition);
        List<PermissionListVO> volist = permissionService.po2vo(poList);
        return volist;
    }

    @Override
    public PageResult<PermissionListVO> pageQuery(PermissionQueryCondition permissionQueryCondition) {
        PageResult<PermissionPO> pageResult = permissionService.pageQuery(permissionQueryCondition);
        List<PermissionListVO> volist = permissionService.po2vo(pageResult.getList());
        PageResult<PermissionListVO> finalResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, finalResult);
        finalResult.setList(volist);
        return finalResult;
    }
}
